//
//  SetTableViewController.m
//  SuiRan
//
//  Created by lanou3g on 15-5-7.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//


#import "SetTableViewController.h"
#import "RESideMenu.h"
#import "AppDelegate.h"
#import <MessageUI/MessageUI.h>

@interface SetTableViewController ()<MFMailComposeViewControllerDelegate>

@property (nonatomic, retain) NSArray *titleArray0;
@property (nonatomic, retain) NSArray *titleArray1;

@end

@implementation SetTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
        
    self.navigationItem.title = @"设置";
    self.titleArray0 = @[@"给个好评", @"关于随然", @"联系我们", @"免责声明"];
    self.titleArray1 = @[@"清除缓存", @"退出程序"];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"reuseIdentifier"];
    
    
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"menu"] style:UIBarButtonItemStylePlain target:self action:@selector(presentLeftMenuViewController:)] autorelease];
}







#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

  
    if (section == 0) {
        return 4;
    } else
        return 2;
  
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier" forIndexPath:indexPath];

    // 点击显示无色
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    switch (indexPath.section) {
        case 0:
            cell.textLabel.text = _titleArray0[indexPath.row];
            break;
            
        case 1:
            cell.textLabel.text = _titleArray1[indexPath.row];
            break;
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80./ 667 *kScreenHeight;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 50. / 667 *kScreenHeight;

}





- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    

    // s == 0 && r == 0
    if (indexPath.section == 0 && indexPath.row == 0) {
        [self comment];
    }
    // s == 0 && r == 3
    if (indexPath.section == 0 && indexPath.row == 2) {

        [self sendEmail];
    }
    
    
    // s == 1 && r == 1
    if (indexPath.section == 1 && indexPath.row == 1) {
        [self alertAndExit];
    }

}

// 0--0 给个好评
- (void)comment
{
//        NSString *str = [NSString stringWithFormat:@"itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=%d", m_appleID];
    NSString *str = [NSString stringWithFormat:@"http://www.mpmp11.com/index.html?ucmidtm=1430992146.36"];
    
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
}

// 0--2 联系我们


#pragma mark - 实现---在程序中发送邮件
- (void)sendEmail {
    // 判断是否可以发送邮件
    if ([MFMailComposeViewController canSendMail]) {
        // 创建邮件视图控制器
        MFMailComposeViewController *picker =[[MFMailComposeViewController alloc] init];
        picker.mailComposeDelegate = self; // 设置代理
        [picker setSubject:@"haha"]; // 设置主题
        [picker setToRecipients:@[@"898@qq.com"]]; // 设置收件人 可以多人发送
        [picker setCcRecipients:@[@"794@qq.com"]]; // 设置抄送对象 同样可以多人发送
        [picker setMessageBody:@"哈哈哈哈哈" isHTML:YES]; // 设置发送的内容
        // 推出视图
        [self presentViewController:picker animated:YES completion:nil];
        [picker release], picker = nil;
        
    } else {
        NSLog(@"无法发送短信");
    }
}
#pragma mark - MFMailComposeViewControllerDelegate Methods
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
    // 判断发送状态
    switch (result) {
        case MFMailComposeResultCancelled:
            NSLog(@"取消发送");
            break;
        case MFMailComposeResultFailed:
            NSLog(@"邮件发送失败");
            NSLog(@"错误信息：%@", error);
            break;
        case MFMailComposeResultSaved:
            NSLog(@"邮件保存");
            break;
        case MFMailComposeResultSent:
            NSLog(@"邮件发送成功");
            break;
        default:
            break;
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}



// 1--3 退出程序选项
- (void)alertAndExit
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"您即将退出随然" preferredStyle:UIAlertControllerStyleAlert];
    [self showDetailViewController:alert sender:nil];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        NSLog(@"取消");
    }];
    [alert addAction:cancelAction];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        
        [self exitApplication];
    }];
    
    [alert addAction:okAction];
}

// 退出程序
- (void)exitApplication {
    AppDelegate *app = [UIApplication sharedApplication].delegate;
    UIWindow *window = app.window;
    
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tree"]];
    imageView.frame = window.frame;
    [window addSubview:imageView];
    
    [UIView animateWithDuration:1.5f animations:^{
        window.alpha = 0;
    } completion:^(BOOL finished) {
        exit(0);
    }];
    
}



- (void)dealloc
{
    [_titleArray0 release];
    [_titleArray1 release];
    [super dealloc];
}

@end
